package com.question.business.dict.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.question.business.dict.entity.DictType;
import com.question.business.dict.mapper.DictTypeMapper;
import com.question.business.dict.model.ao.DictTypeAddAO;
import com.question.business.dict.model.ao.DictTypeEnabledAO;
import com.question.business.dict.model.ao.DictTypePageAO;
import com.question.business.dict.model.ao.DictTypeUpdateAO;
import com.question.business.dict.model.dto.DictCacheDTO;
import com.question.business.dict.model.vo.DictTypeAddVO;
import com.question.business.dict.model.vo.DictTypeDetailVO;
import com.question.business.dict.model.vo.DictTypeVO;
import com.question.business.dict.service.IDictService;
import com.question.business.dict.service.IDictTypeService;
import com.question.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 字典类型服务实现类
 *
 * @author star
 * @since 2023-09-07
 */
@Slf4j
@Service
@AllArgsConstructor
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements IDictTypeService {

    private IDictService dictService;

    /**
     * 字典类型分页查询
     *
     * @param ao 请求参数
     * @return Page<DictTypeVO> 响应参数
     */
    @Override
    public Page<DictTypeVO> queryDictTypePage(DictTypePageAO ao) {
        return baseMapper.queryDictTypePage(ao.createPage(), ao);
    }

    /**
     * 字典类型明细查询
     *
     * @param id 字典类型ID
     * @return DictTypeDetailVO 响应参数
     */
    @Override
    public DictTypeDetailVO queryDictTypeDetail(String id) {
        DictType entity = super.getById(id);
        if (Objects.nonNull(entity)) {
            DictTypeDetailVO vo = new DictTypeDetailVO();
            BeanUtils.copyProperties(entity, vo);
            vo.setDictDetails(dictService.queryDictList(vo.getId()));
            return vo;
        }
        return null;
    }

    /**
     * 字典类型新增数据
     *
     * @param ao 请求参数
     * @return DictTypeAddVO 响应参数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public DictTypeAddVO addDictType(DictTypeAddAO ao) {
        boolean isExist = SqlHelper.retBool(baseMapper.queryByType(ao.getDictType()));
        if (isExist) {
            throw new BusinessException("字典已存在");
        }
        DictType entity = new DictType();
        BeanUtils.copyProperties(ao, entity);
        super.save(entity);
        // 增加字典明细
        dictService.addDict(entity.getId(), ao.getDictDetails());
        DictTypeAddVO vo = new DictTypeAddVO();
        vo.setId(entity.getId());
        return vo;
    }

    /**
     * 字典类型修改数据
     *
     * @param ao 数据
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDictType(DictTypeUpdateAO ao) {
        DictType entity = new DictType();
        BeanUtils.copyProperties(ao, entity);
        super.updateById(entity);
        // 修改字典明细
        dictService.addDict(entity.getId(), ao.getDictDetails());
    }

    /**
     * 字典类型删除数据
     *
     * @param id 字典类型ID
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDictType(String id) {
        super.removeById(id);
        // 删除字典明细
        dictService.addDict(id, null);
    }

    /**
     * 字典类型启用、停用
     *
     * @param ao 数据
     */
    @Override
    public void enableDictType(DictTypeEnabledAO ao) {
        this.lambdaUpdate().eq(DictType::getId, ao.getId()).set(DictType::getEnabled, ao.getEnabled()).update(new DictType());
    }

    /**
     * 查询用户缓存
     *
     * @return List<DictCacheDTO>
     */
    @Override
    public List<DictCacheDTO> queryDictCache() {
        return baseMapper.queryDictCache();
    }

}